#include "Weighted_Directed_Graph.hpp"


/**
 * @brief 通过邻接矩阵构建邻接表表示的带权有向图
 * @param e 输入的邻接矩阵
 */
Weight_Directed_Graph:: Weight_Directed_Graph(vector<vector<int>> e)
{
    //E = e;
    n_V = e.size();
    for (size_t i = 0; i < n_V; i++)
    {
        auto v = new vertex;
        V.push_back(v);
        V[i]->index = i;
        vertex* p = V[i];
        for (size_t j = 0; j < n_V; j++)
        {
            if (e[i][j] != 0)
            {
                p->next = new vertex;
                p->next->index = j;
                p->next->next = nullptr;
                p = p->next;
            }
        }
    }
}


/**
 * @brief 通过邻接表构建带权有向图
 * 
 * @param V 邻接表。邻接表中节点指向的地址不会被重新分配。
 */
Weight_Directed_Graph::Weight_Directed_Graph(vector<vertex* > V){
    this->V = V;
    this->n_V = V.size();
}

/**
 * @brief Destroy the Weight_Directed_Graph object
 * 
 */
Weight_Directed_Graph::~Weight_Directed_Graph(){
    if (need_free_memory)
    {
        for (size_t i = 0; i < n_V; i++)
        {
            vertex* p = V[i]->next;
            while (p != nullptr)
            {
                vertex* p_pre = p;
                p = p->next;
                delete p_pre;
            }
        }
    }
}